package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.mapper.SpuDetailMapper;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SpuDetailServiceImpl extends ServiceImpl<SpuDetailMapper,SpuDetail> implements SpuDetailService {
   @Autowired
    SpecParamService specParamService;
   @Autowired
    SpuService spuService;
    @Override
    public  List<SpecParamDTO> querySpecParam(Long id, boolean searching) {
            //1.查询spudetail
        SpuDetail byId = getById(id);
            //转换map集合 key是SpuDetail的id value 是规格参数
        Map<Long, Object> longObjectMap = JsonUtils.toMap(byId.getSpecification(), Long.class, Object.class);

        //获取商品id
        SpuDTO spuDTO = spuService.querySpuById(byId.getSpuId());

        //通过商品id查询规格参数
        List<SpecParamDTO> list = specParamService.queryParams(spuDTO.getCid3(), null, searching);
        for (SpecParamDTO specParamDTO : list) {
            specParamDTO.setValue(longObjectMap.get(specParamDTO.getId()));

        }
        return list;
    }
}
